/**
 * Copyright (c) 2012, Hakans Pirates - 
 * Johan Andersson spaiki17@gmail.com, 
 * Mattias Lundberg lundberg.mattias@gmail.com, 
 * Samuel Sjodin sjoodin@gmail.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package pirates.inputDevices;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.bullet.control.VehicleControl;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.scene.Node;
import pirates.Settings;
import pirates.chat.ChatDisplay;
import pirates.chat.ChatState;
import pirates.notifications.Notify;

/**
 * @deprecated use KeyMouseControl instead!
 * @author Johan
 */
public class KeyMouseState extends AbstractAppState implements ActionListener {

	private SimpleApplication app;
	private InputManager inputManager;
	private VehicleControl player;
	private Node shipNode;
	private SimpleApplication simApp;
	private ChatDisplay chat;
	private ChatState chatState = null;

	/**
	 * @deprecated use KeyMouseControl instead!
	 * @param app
	 * @param chat
	 * @param player
	 */
	public KeyMouseState(SimpleApplication app, ChatDisplay chat,
			VehicleControl player) {
		this.simApp = app;
		this.player = player;
		this.chat = chat;
	}

	/**
	 * @deprecated use KeyMouseControl instead!
	 * @param enabled
	 */
	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);

		if (enabled) {
			Notify.debug("steering enabled!");
			inputManager.addMapping("ShipLeft", new KeyTrigger(KeyInput.KEY_A));
			inputManager
					.addMapping("ShipRight", new KeyTrigger(KeyInput.KEY_D));
			inputManager.addMapping("ShipForward", new KeyTrigger(
					KeyInput.KEY_W));
			inputManager.addMapping("ShipBackward", new KeyTrigger(
					KeyInput.KEY_S));
			inputManager.addMapping("ShipJump", new KeyTrigger(
					KeyInput.KEY_RETURN));
			inputManager.addMapping("ShipAttack", new KeyTrigger(
					KeyInput.KEY_SPACE));
			inputManager.addMapping("chat_command", new KeyTrigger(
					KeyInput.KEY_TAB));

			inputManager.addListener(this, "ShipLeft", "ShipRight");
			inputManager.addListener(this, "ShipForward", "ShipBackward");
			inputManager.addListener(this, "ShipAttack");
			inputManager.addListener(this, "chat_command");
		} else {
			inputManager.deleteMapping("ShipLeft");
			inputManager.deleteMapping("ShipRight");
			inputManager.deleteMapping("ShipForward");
			inputManager.deleteMapping("ShipBackward");
			inputManager.deleteMapping("ShipAttack");
			inputManager.deleteMapping("chat_command");
			inputManager.removeListener(this);
		}
	}

	/**
	 * @deprecated use KeyMouseControl instead!
	 * @param stateManager
	 * @param app
	 */
	@Override
	public void initialize(AppStateManager stateManager, Application app) {
		super.initialize(stateManager, app);

		this.app = (SimpleApplication) app;
		this.inputManager = this.app.getInputManager();
		this.shipNode = (Node) this.app.getRootNode().getChild(
				Settings.SHIP_NODE);
		setEnabled(true);
	}

	/**
	 * @deprecated use KeyMouseControl instead!
	 * @param name
	 * @param isPressed
	 * @param tpf
	 */
	public void onAction(String name, boolean isPressed, float tpf) {

		if (name.equals("ShipLeft") && isPressed) {
			// player.setLinearVelocity(Vector3f.UNIT_X.mult(10));
		}

		if (name.equals("chat_command") && isPressed) {
			setEnabled(false);
			if (chatState == null) {
				chatState = new ChatState(chat, null);
				chatState.initialize(app.getStateManager(), app);
				app.getStateManager().attach(chatState);
			}
			chatState.setEnabled(true);
		}
	}
}
